
#include "ddx_flie.h"
#include "System.h"


FILE_ddx* fopen_ddx( char *filename, const char *mode )
{
	FILE_ddx* fp=0;
	HANDLE filehandle;
	char  	filepath[128];
	TCHAR	File[128];
	ChangePath(filepath, filename);
	GetUnicodeString(File, (LPCSTR)filepath, 0 );
	
	if (mode[0]=='w' && mode[1]=='b')
	{
		
		filehandle =CreateFile((LPCTSTR)File,  GENERIC_WRITE,  FILE_SHARE_WRITE, NULL
			, CREATE_ALWAYS , FILE_ATTRIBUTE_NORMAL, (HANDLE)NULL);
		if (filehandle!=INVALID_HANDLE_VALUE)
		{
			fp=new FILE_ddx;
			fp->filehandle=filehandle;
		}
	}
	else if (mode[0]=='r' && mode[1]=='b')
	{
		filehandle =CreateFile((LPCTSTR)File, GENERIC_READ,FILE_SHARE_READ,
			(LPSECURITY_ATTRIBUTES)NULL,OPEN_EXISTING,FILE_ATTRIBUTE_READONLY, (HANDLE)NULL);
		if (filehandle!=INVALID_HANDLE_VALUE)
		{
			fp=new FILE_ddx;
			fp->filehandle=filehandle;
		}
		else
			gm_printf("OPEN not EXISTING file\n");
	}
	
	return fp;
}
void fwrite_ddx(void *ptr, size_t size, size_t n,FILE_ddx *stream)
{
	DWORD reallength;
	WriteFile(stream->filehandle, ptr, size , &reallength,NULL) ;
}
void fread_ddx(void *buffer,size_t size, size_t count,FILE_ddx *stream)
{
	DWORD reallength;
	ReadFile(stream->filehandle, buffer, size , &reallength,NULL) ;
}
void fclose_ddx(FILE_ddx* fp)
{
	if(fp!=0)
	{
		CloseFile(fp->filehandle);
		delete fp;
		
	}
	fp=0;
	
}

FILE_ddx* g_Log[4];

